﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TextBinding.cs" company="">
//   
// </copyright>
// <summary>
//   The text binding.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Libium.Bindings
{
    using Libium.UseCases;
    using Libium.Views;

    /// <summary>
    /// The text binding.
    /// </summary>
    /// <typeparam name="T1">
    /// </typeparam>
    /// <typeparam name="T2">
    /// </typeparam>
    public class TextBinding<T1, T2> : UseCaseBinding<T1, T2>
        where T1 : ITextUseCase where T2 : ITextView
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="TextBinding{T1,T2}"/> class.
        /// </summary>
        /// <param name="service">
        /// The service. 
        /// </param>
        /// <param name="useCase">
        /// The use case. 
        /// </param>
        /// <param name="view">
        /// The view. 
        /// </param>
        public TextBinding(IBindService service, T1 useCase, T2 view)
            : base(service, useCase, view)
        {
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        ///   The bind.
        /// </summary>
        public override void Bind()
        {
            base.Bind();
            SetViewText();
            UseCase.TextChanged += OnElementTextChanged;
        }

        /// <summary>
        ///   The unbind.
        /// </summary>
        public override void Unbind()
        {
            base.Unbind();
            UseCase.TextChanged -= OnElementTextChanged;
        }

        #endregion

        #region Methods

        /// <summary>
        /// The on element text changed.
        /// </summary>
        /// <param name="useCase">
        /// The use case. 
        /// </param>
        private void OnElementTextChanged(ITextUseCase useCase)
        {
            SetViewText();
        }

        /// <summary>
        ///   The set view text.
        /// </summary>
        private void SetViewText()
        {
            View.Text = UseCase.Text ?? "no content";
        }

        #endregion
    }

    /// <summary>
    ///   The text binding.
    /// </summary>
    public class TextBinding : TextBinding<ITextUseCase, ITextView>
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="TextBinding"/> class.
        /// </summary>
        /// <param name="service">
        /// The service. 
        /// </param>
        /// <param name="useCase">
        /// The use case. 
        /// </param>
        /// <param name="view">
        /// The view. 
        /// </param>
        public TextBinding(IBindService service, ITextUseCase useCase, ITextView view)
            : base(service, useCase, view)
        {
        }

        #endregion
    }
}